package com.lzkj.baotouhousingfund.utils;

import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.Priority;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.request.RequestOptions;

import io.reactivex.Observable;
import io.reactivex.schedulers.Schedulers;


/**
 * 图片加载类,统一适配(方便换库,方便管理)
 */
public class ImageLoader {

    public static void load(Activity activity,
                            String url,
                            int loading,
                            int error,
                            final ImageView view) {

        RequestOptions options = new RequestOptions();
        options.centerCrop()
                .placeholder(loading)
                .error(error);

        Glide.with(activity).load(url).apply(options).into(view);
    }

    public static void load(Fragment fragment,
                            String url,
                            int error,
                            int loading,
                            final ImageView view) {

        RequestOptions options = new RequestOptions();
        options.centerCrop()
                .placeholder(loading)
                .error(error);
        Glide.with(fragment).load(url).apply(options).into(view);
    }

    public static void load(Activity activity, Object url, ImageView view) {
        Glide.with(activity).load(url).into(view);
    }

    public static void load(Context context, Object url, ImageView view) {
        Glide.with(context).load(url).into(view);
    }

    public static void loadOverrideAndPriority(Context context,
                                               int resizeX, int resizeY,
                                               Object url, ImageView view) {
        RequestOptions options = new RequestOptions();
        options.override(resizeX, resizeY)
                .priority(Priority.HIGH);
        Glide.with(context).load(url).apply(options).into(view);
    }

    public static void loadGIfOverrideAndPriority(Context context,
                                               int resizeX, int resizeY,
                                               Object url, ImageView view) {
        RequestOptions options = new RequestOptions();
        options.override(resizeX, resizeY)
                .priority(Priority.HIGH);
        Glide.with(context).asGif().load(url).apply(options).into(view);
    }

    public static void loadGif(Activity activity, Object url, int error, ImageView view) {

        RequestOptions options = new RequestOptions();
        options.centerCrop()
                .error(error)
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(activity).asGif().load(url).apply(options).into(view);
    }

    public static void loadBitmap(Context context,
                                  int resize,
                                  Drawable placeholder,
                                  ImageView imageView,
                                  Object url) {
        RequestOptions options = new RequestOptions();
        options.centerCrop()
                .placeholder(placeholder)
                .override(resize);
        Glide.with(context).asBitmap().load(url).apply(options).into(imageView);
    }


    /**
     * 自定义圆形裁剪.
     */
    public static void loadCircle(Context context,
                                  Object url, int error, int loading,
                                  ImageView view) {

        RequestOptions options = new RequestOptions();
        options.centerCrop()
                .error(error)
                .transform(new CircleTransform(context))
                .diskCacheStrategy(DiskCacheStrategy.ALL);
        Glide.with(context).asGif().load(url).apply(options).into(view);
    }


    public static void clear(Context context) {
        Observable.just(1)
                .observeOn(Schedulers.io())
                .subscribe(integer -> {
                    Glide.get(context).clearMemory();
                    Glide.get(context).clearDiskCache();
                });
    }
}
